package com.galudisu.handsonic.config;

import com.galudisu.handsonic.base.Constants;
import com.galudisu.handsonic.base.utils.IdWorker;
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import java.util.Locale;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.galudisu.handsonic.modules"})
public class Config {
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages/messages");
        messageSource.setDefaultEncoding(Constants.ENCODING);
        return messageSource;
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource());
        return validator;
    }

    @Bean
    public CookieLocaleResolver resolver() {
        CookieLocaleResolver resolver = new CookieLocaleResolver();
        resolver.setCookieName("language");
        resolver.setCookieMaxAge(100000);
        resolver.setCookiePath("/");
        resolver.setDefaultLocale(Locale.ENGLISH);
        return resolver;
    }

}
